package com.chen.controller.admin;


import com.alibaba.fastjson.JSON;
import com.chen.pojo.Menu;
import com.chen.pojo.Vip;
import com.chen.service.menu.MenuService;
import com.chen.util.*;
import com.chen.vo.MenuVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.*;


@RestController
@RequestMapping("/admin/menu")
public class MenuController {

    //注入MenuService
    @Resource
    private MenuService menuService;

    //加载首页左侧菜单
    @RequestMapping(value = "/loadMenuList",produces = {"application/json;charset=UTF-8"})
    public String loadMenuList(HttpSession session){
        //为什么创建三个集合，是因为init json 文件里面有三兄弟
        //创建map集合，保存menuInfo信息
        Map<String,Object> map = new LinkedHashMap<String, Object>();
        //创建map集合，保存homeinfo信息
        Map<String,Object> homeInfo = new LinkedHashMap<String, Object>();
        //创建map集合，保存logoInfo信息
        Map<String,Object> logoInfo = new LinkedHashMap<String, Object>();
        //查询所有菜单列表方法
       // List<Menu> menuList = menuService.queryAllMenu();
        Vip vip = (Vip) session.getAttribute(SystemConstrant.LOGINUSER);
        List<Menu> menuList = menuService.queryMenuListByEmployeeId(vip.getVipId());
        //创建集合，保存菜单关系
        List<MenuNode> menuNodeList = new ArrayList<MenuNode>();

        //循环菜单列表，创建菜单之间的层级关系
        for (Menu menu : menuList) {
            //创建节点对象
            MenuNode menuNode = new MenuNode();
            menuNode.setHref(menu.getHref());//菜单链接
            menuNode.setIcon(menu.getIcon());//菜单图标
            menuNode.setId(menu.getId());//菜单编号
            menuNode.setPid(menu.getPid());//菜单父id
            menuNode.setRemark(menu.getRemark());//菜单备注信息
            menuNode.setTarget(menu.getTarget());//菜单链接打开方式
            menuNode.setTitle(menu.getTitle());//菜单名称
            //将对象添加到集合
            menuNodeList.add(menuNode);
        }


        //保存homeInfo信息
        homeInfo.put("title","首页");
        homeInfo.put("href","/admin/desktop");
        //保存logoInfo信息
        logoInfo.put("title","球场预定系统");//logo标题
        logoInfo.put("image","/statics/layui/images/logo.png");//logo照片
        logoInfo.put("href","/admin/home.html");//首页地址
        //将菜单信息添加到MenuInfo集合中
        map.put("menuInfo", TreeUtil.toTree(menuNodeList,0));
        map.put("homeInfo",homeInfo);
        map.put("logoInfo",logoInfo);


        //所有的数据都是保存在map里面
        return JSON.toJSONString(map);

    }

    /**
     * 加载菜单管理页面的左侧导航树
     * @return
     */
    @RequestMapping("/loadMenuTree")
    public DataGridViewResult loadMenuTree(){
        //调用查询所有菜单列表的方法
        List<Menu> menuList = menuService.queryAllMenu();
        //创建集合保存节点信息
        List<TreeNode> treeNodes = new ArrayList<TreeNode>();
        //循环遍历菜单列表集合
        for (Menu menu : menuList) {
            //判断当前菜单是否展开
            Boolean spread = (menu.getSpread()==null || menu.getSpread()==1) ? true : false;
            //将菜单信息保存到treeNodes集合中
            treeNodes.add(new TreeNode(menu.getId(),menu.getPid(),menu.getTitle(),spread));
        }
        //返回数据
        return new DataGridViewResult(treeNodes);
    }

    /**
     * 分页查询菜单列表
     * @param menuVo
     * @return
     */
    @RequestMapping("/list")
    public DataGridViewResult list(MenuVo menuVo){
        //设置分页信息
        PageHelper.startPage(menuVo.getPage(),menuVo.getLimit());
        //调用查询菜单列表的方法
        List<Menu> menuList = menuService.findMenuListByPage(menuVo);
        //创建分页对象
        PageInfo<Menu> pageInfo = new PageInfo<Menu>(menuList);
        //返回数据
        return new DataGridViewResult(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 添加一个菜单
     * @param menu
     * @return
     */
    @RequestMapping(value = "/addMenu",produces = {"application/json;charset=UTF-8"})
    public String addMenu(Menu menu){
        Map<String, Object> map = new HashMap<String, Object>();
        System.out.println(menu);
        if (menuService.addMenu(menu)>0){
            map.put(SystemConstrant.SUCCESS,true);
            map.put(SystemConstrant.MESSAGE,"添加成功");
        }else {
            map.put(SystemConstrant.SUCCESS,false);
            map.put(SystemConstrant.MESSAGE,"添加失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 修改一个菜单
     * @param menu
     * @return
     */
    @RequestMapping(value = "/updateMenu",produces = {"application/json;charset=UTF-8"})
    public String updateMenu(Menu menu){
        Map<String, Object> map = new HashMap<String, Object>();
        if (menuService.updateMenu(menu)>0){
            map.put(SystemConstrant.SUCCESS,true);
            map.put(SystemConstrant.MESSAGE,"修改成功");
        }else {
            map.put(SystemConstrant.SUCCESS,false);
            map.put(SystemConstrant.MESSAGE,"修改失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 判断该菜单是否有子菜单
     * @param id
     * @return
     */
    @RequestMapping(value = "/checkMenuHasChild",produces = {"application/json;charset=UTF-8"})
    public String checkMenuHasChild(Integer id){
        Map<String,Object> map = new HashMap<String,Object>();
        //调用查询菜单的方法
        if(menuService.getMenuCountByMenuId(id)>0){
            map.put(SystemConstrant.EXIST,true);
            map.put(SystemConstrant.MESSAGE,"该菜单下有子菜单，无法删除");
        }else{
            map.put(SystemConstrant.EXIST,false);
        }
        return JSON.toJSONString(map);
    }

    /**
     * 删除一个菜单
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteMenu",produces = {"application/json;charset=UTF-8"})
    public String deleteMenu(Integer id){
        Map<String, Object> map = new HashMap<String, Object>();
        if (menuService.deleteMenu(id)>0){
            map.put(SystemConstrant.SUCCESS,true);
            map.put(SystemConstrant.MESSAGE,"删除成功");
        }else {
            map.put(SystemConstrant.SUCCESS,false);
            map.put(SystemConstrant.MESSAGE,"删除失败");
        }
        return JSON.toJSONString(map);
    }
}
